package com.atguigu.realtime.app;

import org.apache.flink.configuration.Configuration;
import org.apache.flink.runtime.state.filesystem.FsStateBackend;
import org.apache.flink.streaming.api.CheckpointingMode;
import org.apache.flink.streaming.api.environment.CheckpointConfig;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;

/**
 * @Author lizhenchao@atguigu.cn
 * @Date 2021/4/27 9:22
 */
abstract public class BaseSqlApp {
    
    protected abstract void run(StreamTableEnvironment tEnv);
    
    public void init(int port, int parallelism, String ck) {
        System.setProperty("hadoop_user_name", "atguigu");
        // 1. 消费Kafka数据
        Configuration conf = new Configuration();
        conf.setInteger("rest.port", port);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        env.setParallelism(parallelism);
        
        env.setStateBackend(new FsStateBackend("hdfs://hadoop162:8020/gmall1026/" + ck));
        env.enableCheckpointing(1000 * 20, CheckpointingMode.EXACTLY_ONCE);
        env.getCheckpointConfig().setCheckpointTimeout(60000);
        
        env.getCheckpointConfig()
            .enableExternalizedCheckpoints(CheckpointConfig.ExternalizedCheckpointCleanup.RETAIN_ON_CANCELLATION);
        
        StreamTableEnvironment tEnv = StreamTableEnvironment.create(env);
        run(tEnv);
        
        
    }
}
